<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${package_name}.dao.${table_name}Dao">
    <resultMap id="${table_name}ResultMap" type="${package_name}.po.${table_name}PO">
        <#list model_column as model>
            <result property="${model.changeColumnName?uncap_first}" column="${model.changeColumnName?uncap_first}"/>
        </#list>
    </resultMap>

    <sql id="columns">
    <#list model_column as model>
        ${model.columnName} AS ${model.changeColumnName?uncap_first}<#if model_index + 1 < model_column?size >,</#if>
    </#list>
    </sql>

    <sql id="where">
        <where>
            <#list model_column as model>
                <if test="${model.changeColumnName?uncap_first} != null">
                    AND ${model.columnName} =  ${r"#{" + "${model.changeColumnName?uncap_first}" + "}"}
                </if>
            </#list>
        </where>
    </sql>

    <select id="getById" parameterType="String" resultType="${package_name}.po.${table_name}PO">
        select
        <include refid="columns"/>
        from
        ${table_name_small}
        where
        id = ${r'#{id}'}
    </select>

    <select id="getByCondition" resultType="${package_name}.po.${table_name}PO">
        select
        <include refid="columns"/>
        from
        ${table_name_small}
        <include refid="where"/>
    </select>

    <select id="queryList" resultMap="${table_name}ResultMap">
        select
        <include refid="columns"/>
        from
        ${table_name_small}
        <include refid="where"/>
    </select>

    <select id="queryPage" resultMap="${table_name}Map">
        SELECT
        <include refid="columns"/>
        FROM
        ${table_name_small}
        <include refid="where"/>
    ${limit}
    </select>

    <select id="countQueryPage" resultType="long">
        SELECT
        COUNT(1)
        FROM
        ${table_name_small}
        <include refid="where"/>
    </select>


    <select id="count" resultType="long">
        SELECT
        COUNT(1)
        FROM
        ${table_name_small}
        <include refid="where"/>
    </select>


    <insert id="save">
        INSERT INTO ${table_name_small}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list model_column as model>
                <if test="${model.changeColumnName?uncap_first} != null">
                    ${model.columnName},
                </if>
            </#list>
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
    	<#list model_column as model>
            <if test="${model.changeColumnName?uncap_first} != null">
                ${r"#{" +  "${model.changeColumnName?uncap_first}" + "}"},
            </if>
        </#list>
        </trim>
    </insert>

    <insert id="saveBatch">
        INSERT INTO ${table_name_small} (
        <#list model_column as model>
            ${model.columnName}<#if model_index + 1 < model_column?size>,</#if>
        </#list>
        ) VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (
	        <#list model_column as model>
                ${r"#{" +  "${model.changeColumnName?uncap_first}" + "}"}<#if model_index + 1 < model_column?size>,</#if>
            </#list>
            )
        </foreach>
    </insert>

    <update id="updateById">
        UPDATE ${table_name_small}
        <set>
        	<#list model_column as model>
                <if test="${model.changeColumnName?uncap_first} != null">
                    ${model.columnName} = ${r"#{" + "${model.changeColumnName?uncap_first}" + "}"}<#if model_index + 1 < model_column?size>,</#if>
                </if>
            </#list>
        </set>
        where
        id = ${r'#{id}'}
    </update>

    <delete id="deleteById">
        DELETE FROM
        ${table_name_small}
        where
        id = ${r'#{id}'}
    </delete>

</mapper>
